private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
toArray 通常需要进行类型强转
1 2 3
public Object[] toArray() { return Arrays.copyOf(elementData, size); }
获取某个索引的元素,我们看到也是强转类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
E elementData(int index) { return (E) elementData[index]; }
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index);
return elementData(index); }
还有 set 方法,覆盖之前的值,返回旧值
1 2 3 4 5 6 7
public E set(int index, E element) { rangeCheck(index);
E oldValue = elementData(index); elementData[index] = element; return oldValue; }
插入,需要移动之后的所有元素
1 2 3 4 5 6 7 8 9
public void add(int index, E element) { rangeCheckForAdd(index);
int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work
return oldValue; }
删除某个 object,看到了 fastRemove,只不过是去除了边界验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; }
LinkedList
由于是链表,很多首尾节点的插入删除操作,方便了很多
属性
很简单的三个属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
transient int size = 0;
/** * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Node<E> first;
/** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node<E> last;
node 静态内部类,根据 Effective Java 的描述,这种设计是比较好的。
1 2 3 4 5 6 7 8 9 10 11
private static class Node<E> { E item; Node<E> next; Node<E> prev;
public boolean add(E e) { linkLast(e); return true; }
void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }
获取元素,需要注意的是为了加快速度,会判断更接近后边还是前边,这样能省一般时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public E get(int index) { checkElementIndex(index); return node(index).item; }
Node<E> node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
而 indexOf(Object o) 就没这个幸运了,必须老老实实从头开始
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
一些关于队列的操作,注意方法的返回值,如果出现为空是否会抛出异常呢,像 element pop remove 会抛异常,而 peek,poll 则不会,这些都要注意,否则一不小心就会引发惨案!!!
public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; }
/** * Retrieves, but does not remove, the head (first element) of this list. * * @return the head of this list * @throws NoSuchElementException if this list is empty * @since 1.5 */ public E element() { return getFirst(); }
public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; }
/** * Retrieves and removes the head (first element) of this list. * * @return the head of this list, or {@code null} if this list is empty * @since 1.5 */ public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }